package ch.rheintalconnection.sysventory.technicalServices.wmi;

import ch.rheintalconnection.sysventory.dto.NicDto;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

public class NICScan {

	private ActiveXComponent axWMI;
	
	public static final String QUERY = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'";
	
	
	public NICScan(ActiveXComponent axWMI){
		this.axWMI = axWMI;
		
	}
	
	public NicDto scan() {
		
		NicDto nicDto = null;
		
		Variant vCollection = axWMI.invoke("ExecQuery", new Variant(QUERY));
		EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
		Dispatch item = null;
		
		while (enumVariant.hasMoreElements()) {
			item = enumVariant.nextElement().toDispatch();
			
			String ipAddress = Dispatch.call(item, "IPAddress").toString();
			String description = Dispatch.call(item, "Description").toString();
			String gateway = Dispatch.call(item, "DefaultIPGateway").toString();
			String macAddress = Dispatch.call(item, "MACAddress").toString();
			String dhcp = Dispatch.call(item, "DHCPServer").toString();
			String subNet = Dispatch.call(item, "IPSubnet").toString();
			
			nicDto = new NicDto(ipAddress, description, gateway, macAddress, dhcp, subNet);
			return nicDto;
		}
		
		return null;
	}
	
}
